import Ember from 'ember';
import BaseBusiness from '../base-business';
const {leaveStatus1,leaveStatus2,leaveStatus3,leaveStatus4,leaveStatus5,
  consultStatus5,consultStatus4,consultStatus6,consultStatus7,leaveReason} = Constants;

export default BaseBusiness.extend({
  constants: Constants,
  dateService: Ember.inject.service("date-service"),
  queryParams: {
      customerflowId: {
          refreshModel: true
      },
      customerId: {
          refreshModel: true
      },
      editMode: {
          refreshModel: true
      },
  },
  header_title:'修改退住登记',
  model(){
    return{};
  },
  setupController(controller, model){
    let _self = this;
    this._super(controller, model);
    var editMode = controller.get('editMode');
    let today = this.get('dateService').getCurrentTime()-1;
    today = this.get("dateService").timestampToTime(today);
    if(editMode != "add"){
      var customerId = controller.get('customerId');
      console.log("customerId in if:",customerId);
      this.store.findRecord("customer",customerId).then(function(customerMessage){
        controller.set('customerMessage',customerMessage);
      });

      //查询账户表
      this.store.query('tradeaccount', {
        filter: {
          '[customer][id]': customerId,
          '[type][typecode]': Constants.accountType1
        }
      }).then(function(actualDepositList) {
        controller.set('actualDepositInfo', actualDepositList.get('firstObject'));
      });
      this.store.query('tradeaccount', {
        filter: {
          '[customer][id]': customerId,
          '[type][typecode]': Constants.accountType2
        }
      }).then(function(balanceList) {
        controller.set('balanceInfo', balanceList.get('firstObject'));
      });
    }


    controller.set('today',today);
    var customerflowId = controller.get('customerflowId');
    if(editMode=='edit'){
      controller.set('lookDetail',false);
      controller.set('detailEdit',false);
      var customerflowedit = this.store.peekRecord("customerbusinessflow",customerflowId);
      controller.set('customerflow',customerflowedit);
      // this.store.findRecord('customerbusinessflow',id).then(function(customerflow){
      //   controller.set('customerflow',customerflow);
      //   console.log("customerflow11111111",customerflow);
      // });
    }else if(editMode=='look'){
      controller.set('detailEdit',false);
      controller.set('lookDetail',true);
      var customerflowlook = this.store.peekRecord("customerbusinessflow",customerflowId);
      controller.set('customerflow',customerflowlook);
      // this.store.findRecord('customerbusinessflow',id).then(function(customerflow){
      //   controller.set('customerflow',customerflow);
      //   console.log("customerflow11111111",customerflow);
      // });
    }
    else{
      controller.set('lookDetail',false);
      controller.set('detailEdit',true);
      var customerflow = this.store.createRecord('customerbusinessflow',{});

      // controller.set('customerflow.leaveStaff','');
      // controller.set('customerflow.leaveReason','');
      // controller.set('customerflow.leaveRemark','');
      controller.set('customerflow',customerflow);

    }

    this.store.query('employee',{filter:{
      staffStatus:{'typecode@$not':Constants.staffStatusLeave}
    }}).then(function(userList){
      userList.forEach(function(item){
        item.set("namePinyin",item.get("name"));
      });
      controller.set('userList',userList);//登记人  护工
      console.log("userList11111111",userList);
    });


    this.store.query('customerbusinessflow',{
      filter:{
        'status---1':{'typecode@$or1---1':Constants.consultStatus4},//试住
        'status---2':{'typecode@$or1---2':Constants.consultStatus5},//入住
        'status---3':{'typecode@$or1---3':Constants.consultStatus6},//退住
        'status---4':{'typecode@$or1---4':Constants.consultStatus7},//作废
        customer:{id:controller.get('customerId')}
      }
    }).then(function(customerbusinessflowList){
      let customerbusinessflowItem = customerbusinessflowList.get("firstObject");
      controller.set('customerbusinessflowItem',customerbusinessflowItem);
      controller.checkInReferencePriceObs();
      console.log('customerbusinessflowList:',customerbusinessflowList);
      console.log('customerbusinessflowItem:',customerbusinessflowItem);
      let chargeType = customerbusinessflowItem.get('chargeType');
      if(chargeType.get('typecode')=="chargeTypeY"){
        controller.set('chargeMonth',true);
        controller.set('chargeDay',false);
      }
      if(chargeType.get('typecode')=="chargeTypeD"){
        controller.set('chargeMonth',false);
        controller.set('chargeDay',true);
      }
      console.log('入住偏好：',controller.get('customerbusinessflowInfo.inPreference'));
    });
    this.store.query('customerbusinessflow',{
      filter:{
        'status---1':{'typecode@$or1---1':Constants.consultStatus5},//入住
        'status---2':{'typecode@$or1---2':Constants.consultStatus4},//试住
      }
    }).then(function(customerflowInBedList){
      controller.set('customerflowInBedList',customerflowInBedList);//入住试住 老人customerflow
      controller.incrementProperty("inBedFlag");
    });
    this.store.query('customerbusinessflow',{
      filter:{
        'leaveStatus---1':{'typecode@$or1---1':Constants.leaveStatus1},
        'leaveStatus---2':{'typecode@$or1---2':Constants.leaveStatus2},
        'leaveStatus---3':{'typecode@$or1---3':Constants.leaveStatus3},
        'leaveStatus---4':{'typecode@$or1---4':Constants.leaveStatus4},
      }
    }).then(function(leaveList){
      controller.set('leaveList',leaveList);//退住老人(不包括作废)
      controller.incrementProperty("leaveFlag");
    });


  }
});
